<!-- left1 动态刷新饼形图数据 -->
<template>
  <div ref="refChart" :style="{ height: kHeight + 'px'}"></div>
</template>

<script>
// import { getModuleData } from "../../../api/ems/index";
require("../assets/theme/shine")
const hubei = require("../assets/map/hubei.json")
export default {
  name: '',
  components: {},
  data() {
    return {
      // 获取浏览器可视区域高度（包含滚动条）、 window.innerHeight
      // 获取浏览器可视区域高度（不包含工具栏高度）、document.documentElement.clientHeight
      // 获取body的实际高度  (三个都是相同，兼容性不同的浏览器而设置的) document.body.clientHeight
      screenHeight: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight,
      screenWidth: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
      chartInstance: null,
      allData: [],
      kHeight: null,
      koiTime: null,
      // 局部刷新定时器
      koiTimer: null,
    }
  },
  created() {
  },
  beforeMount() {
    this.kHeight = Math.round(this.screenHeight * 0.945)
  },
  mounted() {
    // 页面大小改变时触发
    window.addEventListener('resize', this.getScreenHeight, false);
    // 页面大小改变时触发
    window.addEventListener('resize', this.getScreenWidth, false);
    // 鼠标移动时触发
    //window.addEventListener('mousemove',this.getHeight, false);
    this.resizeScreen();
    // 图表初始化
    this.initChart();
    this.getData();
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
    // 局部刷新定时器
    this.getDataTimer();
  },
  beforeDestroy() {
    // 销毁Echarts图表
    this.chartInstance.dispose();
    this.chartInstance = null;
  },
  destroyed() {
    // 清除自适应屏幕定时器
    clearInterval(this.koiTime);
    this.koiTime = null;
    // 清除局部刷新定时器
    clearInterval(this.koiTimer);
    this.koiTimer = null;
    // 页面大小改变时触发
    window.removeEventListener('resize', this.getScreenHeight, false);
    // 页面大小改变时触发
    window.removeEventListener('resize', this.getScreenWidth, false);
    window.removeEventListener("resize", this.screenAdapter);
  },
  methods: {
    resizeScreen() {
      this.koiTime = setInterval(() => {
        this.getScreenHeight();
        this.getScreenWidth();
      }, 200)
    },
    // 获取浏览器高度进行自适应
    getScreenHeight() {
      this.screenHeight = window.innerHeight || document.documentElement.innerHeight || document.body.clientHeight;
      // 四舍五入取整数
      this.kHeight = Math.round(this.screenHeight * 0.945);
      //console.log("高度->"+this.screenHeight +"-"+ this.kHOne);
    },
    // 字体大小根据宽度自适应
    getScreenWidth() {
      this.screenWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
      //console.log("宽度->"+this.screenWidth);
    },
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.refChart, 'shine');
      this.$echarts.registerMap('hubei', hubei)
      const initOption = {
        title: {
          text: '湖北银行网点分布数据',
          left: 'center'
        },
        visualMap: {
          min: 0,
          max: 1000,
          right: 50,
          bottom: 20,
          realtime: false,
          calculable: true,
          itemWidth: 10,
          itemHeight: 70,
          inRange: {
            color: ['#ffffff', '#90EE90', '#7CFC00', '#006400']
          }
        },
        series: [
          {
            type: "map",
            map: 'hubei',
            geoIndex: 0,
            label: {
              show: true
            }
          }
        ],
        // 悬浮窗
        tooltip: {
          trigger: 'item',
          confine: true,
          formatter: function (params) {
            return params.name + ":" + params.value;
          }
        },
        geo: {
          map: 'hubei',
          zoom: 1,
          roam: 'move',
          label: {
            show: true,
            position: 'inside',
            distance: 0,
            fontSize: 10,
            rotate: 45,
            color: 'red'
          },
          layoutCenter: ["50%", "50%"],
          //地图尺寸
          layoutSize: "120%",
          // 缩放
          aspectScale: 1,
          itemStyle: {
            //区域边框宽度
            borderWidth: 0.5,
            //区域边框颜色
            borderColor: "#f79e07",
            //区域颜色
            areaColor: "#D3D3D3",
          },
          emphasis: {
            // 鼠标悬浮效果
            itemStyle: {
              borderWidth: 0.5,
              borderColor: "#33ddff",
              areaColor: "#2f6a87",
            },
            label: {
              //对应的鼠标悬浮效果
              show: true,
              color: "#ffffff",
            }
          }
        }
      }
      // 图表初始化配置
      this.chartInstance.setOption(initOption);

      // 鼠标移入停止定时器
      this.chartInstance.on("mouseover", () => {
        clearInterval(this.koiTimer);
      });

      // 鼠标移入启动定时器
      this.chartInstance.on("mouseout", () => {
        this.getDataTimer();
      });
    },
    getData() {
      // getModuleData().then(res => {
      //     this.allData = res.data;
      //     this.updateChart();
      //     //console.log("数据left1->"+JSON.stringify(this.allData.pcsSum));
      //     // echarts查不到数据，将初始化echarts的方法全部放置到接口方法中即可。
      // })
      // 获取服务器的数据, 对this.allData进行赋值之后, 调用updateChart方法更新图表
      //console.log("ALLDATA->",JSON.stringify(res.data))
      //console.log("ALLDATA->",JSON.stringify(res.allData))
      this.updateChart();
    },
    updateChart() {
      let num1 = this.randomNum(50, 950);
      let num2 = this.randomNum(50, 950);
      let num3 = this.randomNum(50, 950);
      let num4 = this.randomNum(50, 950);
      let num5 = this.randomNum(50, 950);

      let num6 = this.randomNum(50, 950);
      let num7 = this.randomNum(50, 950);
      let num8 = this.randomNum(50, 950);
      let num9 = this.randomNum(50, 950);
      let num10 = this.randomNum(50, 950);

      let num11 = this.randomNum(50, 950);
      let num12 = this.randomNum(50, 950);
      let num13 = this.randomNum(50, 950);
      let num14 = this.randomNum(50, 950);
      let num15 = this.randomNum(50, 950);

      let num16 = this.randomNum(50, 950);
      let num17 = this.randomNum(50, 950);

      this.allData = [];
      this.allData.push({value: num1, name: '武汉市'})
      this.allData.push({value: num2, name: '宜昌市'})
      this.allData.push({value: num3, name: '襄阳市'})
      this.allData.push({value: num4, name: '荆州市'})
      this.allData.push({value: num5, name: '荆门市'})

      this.allData.push({value: num6, name: '咸宁市'})
      this.allData.push({value: num7, name: '恩施土家族苗族自治州'})
      this.allData.push({value: num8, name: '神农架林区'})
      this.allData.push({value: num9, name: '十堰市'})
      this.allData.push({value: num10, name: '随州市'})


      this.allData.push({value: num11, name: '孝感市'})
      this.allData.push({value: num12, name: '潜江市'})
      this.allData.push({value: num13, name: '天门市'})
      this.allData.push({value: num14, name: '仙桃市'})
      this.allData.push({value: num15, name: '黄冈市'})

      this.allData.push({value: num16, name: '鄂州市'})
      this.allData.push({value: num17, name: '黄石市'})

      // 处理图表需要的数据
      const dataOption = {
        series: [
          {
            data: this.allData
          }
        ]
      };
      // 图表数据变化配置
      this.chartInstance.setOption(dataOption);
    },
    screenAdapter() {
      this.titleFontSize = (this.$refs.refChart.offsetWidth / 100) * 2;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: Math.round(this.titleFontSize * 2),
          },
        },
        // 圆点分类标题
        legend: {
          textStyle: {
            fontSize: Math.round(this.titleFontSize * 1.2),
          },
        }
      };
      // 图标自适应变化配置
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    // 定时器
    getDataTimer() {
      this.koiTimer = setInterval(() => {
        // 执行刷新数据的方法
        this.getData();
        //console.log("Hello World")
      }, 3000)
    },

  }
}
</script>
